<?php 
include_once("conexion.class.php");

class Permiso{
 //constructor	
 	var $con;
 	function Permiso(){
 		$this->con=new DBManager;
 	}

	function insertar($campos){
		if($this->con->conectar()==true){			
			return mysql_query("INSERT INTO 
								  `role_permisos`
								(
								  `id_permiso`,
								  `id_role`
								) 
								VALUE (
								  '".$campos[0]."',
								  '".$campos[1]."'
								);");
		}
	}
	
	function actualizar($campos,$id){
			

		if($this->con->conectar()==true){
			//print_r($campos);
			return mysql_query("UPDATE role_permisos SET role = '".$campos[0]."', descripcion='".$campos[1]."' WHERE id = ".$id);
		}
	}
	
	
	function mostrar_permiso($id){
		if($this->con->conectar()==true){
			return mysql_query("SELECT 
								  `id_role`,
								  `id_permiso`, p.`permiso`
								FROM 
								  `role_permisos` rp
								  inner join `roles` r on (r.`id` = rp.`id_role`)
								  inner join `permisos` p on (rp.`id_permiso` = p.`id`) where rp.`id_role` =".$id);
		}
	}

	function mostrar_permisos(){
		if($this->con->conectar()==true){
			return mysql_query("SELECT * FROM roles");
		}
	}

	function eliminar($id){
		if($this->con->conectar()==true){
			return mysql_query("DELETE FROM 
								  `role_permisos` 
								WHERE 
								  `id_role` = ".$id." 
								;");
		}
	}
	
	

	function obtener_permisos($id){
		if($this->con->conectar()==true){
			return mysql_query("SELECT p.`id`, p.`permiso`
								 FROM users u
								inner JOIN `roles` r on (u.`id_role` = r.`id`)
								inner join `role_permisos` rp on (r.`id` = rp.`id_role`)
								inner join `permisos` p on (rp.`id_permiso` = p.`id`)
								where u.`id` = ".$id." ;");
		}
	}
}
?>


